package com.hod.client.frontend;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.Function;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.PaddedPanel;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.grid.AbstractSelectionModel;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.RowLayout;
import com.gwtext.client.widgets.layout.RowLayoutData;
import com.hod.client.backend.orm.client.GearsDataObject;
import com.hod.client.backend.orm.client.GearsDataStore;

public abstract class CRUD extends ModulePanel {
	protected GearsDataStore ds;
	protected String title = "Credits";
	protected int gridHeight = 100;
	protected int gridwidth = 500;
	protected int spacing = 15;
	protected String autoExpandColumn = "descCol";
	protected HODForm formPanel = null;
	protected String gridPanelTitle = "Company Data";
	protected String FieldsetTitle = "Company Details";
	protected int formWidth = 800;
	protected int gridPanelHeight = 300;
	public AbstractGrid gridPanel;
	protected Record actualRecord;
	protected abstract void buildPanelSearch();
	protected abstract AbstractSelectionModel createSelectionModel();
	protected abstract Widget formTabs();
	public abstract HODForm createFormPanel();
	
	public CRUD() {
		// TODO Auto-generated constructor stub
	}
	
	public Store getStore(){
		return gridPanel.store;
	}
	
	public HODForm loadObjectinFormPanel(GearsDataObject object) {
		// TODO Auto-generated method stub
		formPanel = createFormPanel();
		formPanel.setObject(object);
		return formPanel;
	}		

	
	public Panel getViewPanel() {
        if (panel == null) {
        	try {
				panel = new Panel();
				panel.setLayout(new RowLayout());
				createFormPanel();
				buildPanelSearch();
				Panel inner = new Panel();
				inner.setLayout(new ColumnLayout());
				//Panel row = new Panel();
				//row.setLayout(new RowLayout());
				gridPanel.setLayout(new FitLayout());
				//gridPanel.setHeight("100%");
				gridPanel.setTitle(title);
				gridPanel.setSelectionModel(createSelectionModel());
				gridPanel.doOnRender(new Function() {
				    public void execute() {
				    	gridPanel.getSelectionModel().selectFirstRow();
				        actualRecord = gridPanel.getSelectionModel().getSelected();
				    }
				}, 10);
				//row.add(gridPanel, new RowLayoutData("99%"));
				inner.add(gridPanel, new ColumnLayoutData(0.3));
				Widget widget = formTabs();
				if (widget!=null){
					Panel vp = new Panel();
					vp.setLayout(new RowLayout());
					vp.add(formPanel,new RowLayoutData("30%"));
					vp.add(widget);
					inner.add(new PaddedPanel(vp, 0, 10, 0, 0), new ColumnLayoutData(0.7));
				}else
				   inner.add(new PaddedPanel(formPanel, 0, 10, 0, 0), new ColumnLayoutData(0.7));
				panel.add(inner, new RowLayoutData("70%"));
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Window.alert(e.getMessage());
			}
        	        	
        }
        return panel;
       
}

	


}
